#include "mainWindow.h"
#include "./ui_mainWindow.h"
#include <QDebug>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QRadioButton>
#include "myMenuIconStyle.h"
#include "systemInfo.h"
#include "utilities/cpuMemInfo.h"
#include "geomMenu/geomSetupRectangle.h"
#include "geomMenu/geomSetupSquare.h"
#include "geomMenu/geomSetupCircle.h"
#include "geomMenu/geomSetupEllipse.h"
#include "geomMenu/geomSetupUnion.h"
#include "geomMenu/geomSetupIntersection.h"
#include "geomMenu/geomSetupDifference.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    _memLabel = new QLabel();

    initValue();
    initTreeView();
    initStatusBar();
    initSpliter();
    initTreeTagList();
    initConnect();
    ui->comboBox->setCurrentIndex(1); // length unit: m
    ui->lineEdit_author->setText(systemInfo::getUserName());

    _timer = new QTimer(this);

    _timer->start(1000);
    connect(_timer, &QTimer::timeout, this, [=]() {
        double usedMemory = cpuMemInfo::instance().usedMemory()/1024/1024/1024;
        double totalMemory = cpuMemInfo::instance().totalMemory()/1024/1024/1024;
        _memLabel->setText("Mem "+QString::number(usedMemory,'f',1)+"/"+QString::number(totalMemory,'f',1)+" Total");
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initValue()
{
    materialIdx = 0;
    globalSet = new globalSetting();
}

// initialize model tree
void MainWindow::initTreeView()
{
    /*
     * Tree view menu
     */
    _model = new QStandardItemModel(this);

    ui->treeView->setHeaderHidden(true);
    ui->treeView->setModel(_model);
    ui->treeView->expandAll();
    ui->treeView->setSelectionBehavior(QTreeView::SelectRows); // set to select multi rows
    ui->treeView->setSelectionMode(QTreeView::ExtendedSelection); // set to select multi rows
    ui->treeView->setIconSize(QSize(24,24));

    connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &MainWindow::slotMenuPopup); // right menu
    connect(ui->treeView, &QTreeView::clicked, this, &MainWindow::slotTreeViewClick); // on click

    ui->treeView->installEventFilter(this);

    // Define
    QStandardItem *itemDefinitions = new QStandardItem("定义");
    itemDefinitions->setIcon(QIcon(":/Define/resources/Define/define.png"));
    itemDefinitions->setData(false, Qt::UserRole);
    itemDefinitions->setData(TreeTag::DEFINE, Qt::UserRole+1);
    itemDefinitions->setEditable(false);
    _model->appendRow(itemDefinitions);

    QStandardItem *itemDefinitionsSub1 = new QStandardItem("模型信息");
    _defineList << "模型信息";
    itemDefinitionsSub1->setData(false, Qt::UserRole); // deletable or not
    itemDefinitionsSub1->setData(TreeTag::DEFINE, Qt::UserRole+1);
    itemDefinitionsSub1->setData(TreeTag::defineTag::INFORMATION, Qt::UserRole+1);
    itemDefinitionsSub1->setIcon(QIcon(":/Define/resources/Define/parameters.png"));
    itemDefinitionsSub1->setEditable(false);
    itemDefinitions->appendRow(itemDefinitionsSub1);

    QStandardItem *itemDefinitionsSub2 = new QStandardItem("参数 1");
    _defineList << "参数 1";
    itemDefinitionsSub2->setData(false, Qt::UserRole);
    itemDefinitionsSub2->setData(TreeTag::DEFINE, Qt::UserRole+1);
    itemDefinitionsSub2->setData(TreeTag::defineTag::PARAMETER, Qt::UserRole+2);
    itemDefinitionsSub2->setIcon(QIcon(":/Define/resources/Define/parameters.png"));
    itemDefinitionsSub2->setEditable(false);
    itemDefinitions->appendRow(itemDefinitionsSub2);

    // Geometry
    QStandardItem *itemGeometry = new QStandardItem("几何");
    itemGeometry->setIcon(QIcon(":/Geometry/resources/Geometry/geometry.png"));
    itemGeometry->setData(false, Qt::UserRole);
    itemGeometry->setData(TreeTag::GEOMETRY, Qt::UserRole+1);
    itemGeometry->setEditable(false);
    _model->appendRow(itemGeometry);

    // Material
    QStandardItem *itemMaterials = new QStandardItem("材料");
    itemMaterials->setIcon(QIcon(":/Material/resources/Material/material.png"));
    itemMaterials->setData(false, Qt::UserRole);
    itemMaterials->setData(TreeTag::MATERIAL, Qt::UserRole+1);
    itemMaterials->setEditable(false);
    _model->appendRow(itemMaterials);

    // Physics, Heat Transfer
    QStandardItem *itemHeatTransfer = new QStandardItem("固体传热");
    itemHeatTransfer->setIcon(QIcon(":/Physics/resources/Physics/heatTransfer.png"));
    itemHeatTransfer->setData(false, Qt::UserRole);
    itemHeatTransfer->setData(TreeTag::HEAT_TRANSFER, Qt::UserRole+1);
    itemHeatTransfer->setEditable(false);
    _model->appendRow(itemHeatTransfer);

    QStandardItem *itemHeatTransferSub1 = new QStandardItem("固体");
    _htConditionList << "固体";
    itemHeatTransferSub1->setIcon(QIcon(":/Physics/resources/Physics/defaultDomain.png"));
    itemHeatTransferSub1->setData(false, Qt::UserRole);
    itemHeatTransferSub1->setData(TreeTag::HEAT_TRANSFER, Qt::UserRole+1);
    itemHeatTransferSub1->setData(TreeTag::htTag::SOLID, Qt::UserRole+2);
    itemHeatTransferSub1->setEditable(false);
    itemHeatTransfer->appendRow(itemHeatTransferSub1);

    QStandardItem *itemHeatTransferSub2 = new QStandardItem("初始值");
    _htConditionList << "初始值";
    itemHeatTransferSub2->setIcon(QIcon(":/Physics/resources/Physics/defaultDomain.png"));
    itemHeatTransferSub2->setData(false, Qt::UserRole);
    itemHeatTransferSub2->setData(TreeTag::HEAT_TRANSFER, Qt::UserRole+1);
    itemHeatTransferSub2->setData(TreeTag::htTag::INIT, Qt::UserRole+2);
    itemHeatTransferSub2->setEditable(false);
    itemHeatTransfer->appendRow(itemHeatTransferSub2);

    QStandardItem *itemHeatTransferSub3 = new QStandardItem("热绝缘");
    _htConditionList << "热绝缘";
    itemHeatTransferSub3->setIcon(QIcon(":/Physics/resources/Physics/defaultBoundary.png"));
    itemHeatTransferSub3->setData(false, Qt::UserRole);
    itemHeatTransferSub3->setData(TreeTag::HEAT_TRANSFER, Qt::UserRole+1);
    itemHeatTransferSub3->setData(TreeTag::htTag::THERMAL_INSULATION, Qt::UserRole+2);
    itemHeatTransferSub3->setEditable(false);
    itemHeatTransfer->appendRow(itemHeatTransferSub3);

    // Mesh
    QStandardItem *itemMesh = new QStandardItem("网格");
    itemMesh->setIcon(QIcon(":/Mesh/resources/Mesh/mesh.png"));
    itemMesh->setData(false, Qt::UserRole);
    itemMesh->setData(TreeTag::MESH, Qt::UserRole+1);
    itemMesh->setEditable(false);
    _model->appendRow(itemMesh);

    QStandardItem *itemMeshSub1 = new QStandardItem("尺寸");
    _meshSettingList << "尺寸";
    itemMeshSub1->setIcon(QIcon(":/Mesh/resources/Mesh/size.png"));
    itemMeshSub1->setData(false, Qt::UserRole);
    itemMeshSub1->setData(TreeTag::MESH, Qt::UserRole+1);
    itemMeshSub1->setData(TreeTag::meshTag::SIZE, Qt::UserRole+2);
    itemMeshSub1->setEditable(false);
    itemMesh->appendRow(itemMeshSub1);

    // Study
    QStandardItem *itemSolver = new QStandardItem("研究");
    itemSolver->setIcon(QIcon(":/Study/resources/Study/study.png"));
    itemSolver->setData(false, Qt::UserRole);
    itemSolver->setData(TreeTag::MESH, Qt::UserRole+1);
    itemSolver->setEditable(false);
    _model->appendRow(itemSolver);

    QStandardItem *itemSolverSub1 = new QStandardItem("设置");
    itemSolverSub1->setIcon(QIcon(":/Study/resources/Study/setting.png"));
    itemSolverSub1->setData(false, Qt::UserRole);
    itemSolverSub1->setData(TreeTag::MESH, Qt::UserRole+1);
    //itemSolverSub1->setData(TreeTag::studyTag::SETTING, Qt::UserRole+2);
    itemSolverSub1->setEditable(false);
    itemSolver->appendRow(itemSolverSub1);

    // Result
    QStandardItem *itemResult = new QStandardItem("结果");
    itemResult->setIcon(QIcon(":/Result/resources/Result/result.png"));
    itemResult->setData(false, Qt::UserRole);
    itemResult->setData(TreeTag::RESULT, Qt::UserRole+1);
    itemResult->setEditable(false);
    _model->appendRow(itemResult);

    QStandardItem *itemResultSub1 = new QStandardItem("数据集");
    _plotGroupList << "数据集";
    itemResultSub1->setIcon(QIcon(":/Result/resources/Result/dataSet.png"));
    itemResultSub1->setData(false, Qt::UserRole);
    itemResultSub1->setData(TreeTag::RESULT, Qt::UserRole+1);
    itemResultSub1->setEditable(false);
    itemResult->appendRow(itemResultSub1);

    /*
     * Right click menu
     */
    ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
    // customized icon size
    MyMenuIconStyle* myIconStyle = new MyMenuIconStyle(); // customized icon style
    myIconStyle->SetCustomSize(24);
    ui->treeView->setStyle(myIconStyle);

    // Define
    menuDefinitions = new QMenu(ui->treeView);
    menuDefinitions->setStyle(myIconStyle);
    QAction* menuDefinitions1 = new QAction(QIcon(":/Define/resources/Define/parameters.png"),tr("参数"));
    QAction* menuDefinitions2 = new QAction(QIcon(":/Define/resources/Define/function.png"),tr("函数"));
    QAction* menuDefinitions3 = new QAction(QIcon(":/Define/resources/Define/select.png"),tr("选择集"));
    menuDefinitions->addAction(menuDefinitions1);
    menuDefinitions->addAction(menuDefinitions2);
    menuDefinitions->addAction(menuDefinitions3);
    connect(menuDefinitions1, &QAction::triggered, this, &MainWindow::addParameter);
    connect(menuDefinitions2, &QAction::triggered, this, &MainWindow::addFunction);
    connect(menuDefinitions3, &QAction::triggered, this, &MainWindow::addSelectSet);

    // Geometry
    menuGeometry = new QMenu(ui->treeView);
    menuGeometry->setStyle(myIconStyle);
    QAction* menuGeometry1 = new QAction(QIcon(":/Geometry/resources/Geometry/rectangle.png"),tr("长方形"));
    QAction* menuGeometry2 = new QAction(QIcon(":/Geometry/resources/Geometry/square.png"),tr("正方形"));
    QAction* menuGeometry3 = new QAction(QIcon(":/Geometry/resources/Geometry/circle.png"),tr("圆形"));
    QAction* menuGeometry4 = new QAction(QIcon(":/Geometry/resources/Geometry/ellipse.png"),tr("椭圆形"));
    QAction* menuGeometry5 = new QAction(QIcon(":/Geometry/resources/Geometry/polygon.png"),tr("多边形"));
    menuGeometry->addAction(menuGeometry1);
    menuGeometry->addAction(menuGeometry2);
    menuGeometry->addAction(menuGeometry3);
    menuGeometry->addAction(menuGeometry4);
    menuGeometry->addAction(menuGeometry5);
    connect(menuGeometry1, &QAction::triggered, this, &MainWindow::addRectangle);
    connect(menuGeometry2, &QAction::triggered, this, &MainWindow::addSquare);
    connect(menuGeometry3, &QAction::triggered, this, &MainWindow::addCircle);
    connect(menuGeometry4, &QAction::triggered, this, &MainWindow::addEllipse);
    connect(menuGeometry5, &QAction::triggered, this, &MainWindow::addPolygen);

    QAction* menuGeometrySeparator1 = new QAction();
    menuGeometrySeparator1->setSeparator(true);
    menuGeometry->addAction(menuGeometrySeparator1);

    QAction* menuGeometry6 = new QAction(QIcon(":/Geometry/resources/Geometry/segment.png"),tr("线段"));
    QAction* menuGeometry7 = new QAction(QIcon(":/Geometry/resources/Geometry/arc.png"),tr("圆弧"));
    menuGeometry->addAction(menuGeometry6);
    menuGeometry->addAction(menuGeometry7);  
    connect(menuGeometry6, &QAction::triggered, this, &MainWindow::addSegment);
    connect(menuGeometry7, &QAction::triggered, this, &MainWindow::addArc);

    QAction* menuGeometrySeparator2 = new QAction();
    menuGeometrySeparator2->setSeparator(true);
    menuGeometry->addAction(menuGeometrySeparator2);

    QAction* menuGeometry8 = new QAction(QIcon(":/Geometry/resources/Geometry/point.png"),tr("点"));
    menuGeometry->addAction(menuGeometry8);
    connect(menuGeometry8, &QAction::triggered, this, &MainWindow::addPoint);

    QAction* menuGeometrySeparator3 = new QAction();
    menuGeometrySeparator3->setSeparator(true);
    menuGeometry->addAction(menuGeometrySeparator3);

    QAction* menuGeometry9 = new QAction(QIcon(":/Geometry/resources/Geometry/union.png"),tr("并集"));
    menuGeometry->addAction(menuGeometry9);
    connect(menuGeometry9, &QAction::triggered, this, &MainWindow::addUnion);
    QAction* menuGeometry10 = new QAction(QIcon(":/Geometry/resources/Geometry/intersection.png"),tr("交集"));
    menuGeometry->addAction(menuGeometry10);
    connect(menuGeometry10, &QAction::triggered, this, &MainWindow::addIntersection);
    QAction* menuGeometry11 = new QAction(QIcon(":/Geometry/resources/Geometry/difference.png"),tr("差集"));
    menuGeometry->addAction(menuGeometry11);
    connect(menuGeometry11, &QAction::triggered, this, &MainWindow::addDifference);

    // Material
    menuMaterials = new QMenu(ui->treeView);
    menuMaterials->setStyle(myIconStyle);
    QAction* menuMaterials1 = new QAction(QIcon(":/Material/resources/Material/addMaterial.png"),tr("材料库"));
    QAction* menuMaterials2 = new QAction(QIcon(":/Material/resources/Material/newMaterial.png"),tr("空材料"));
    //menuMaterials->addAction("添加材料", this, SLOT(addMaterial()));
    menuMaterials->addAction(menuMaterials1);
    menuMaterials->addAction(menuMaterials2);
    connect(menuMaterials1, &QAction::triggered, this, &MainWindow::openMaterialDB);
    connect(menuMaterials2, &QAction::triggered, this, &MainWindow::addMaterial);

    // Physics, Heat Transfer
    menuHeatTransfer = new QMenu(ui->treeView);
    menuHeatTransfer->setStyle(myIconStyle);
    QAction* menuHeatTransferAction1 = new QAction(QIcon(":/Physics/resources/Physics/domain.png"),tr("热源"));
    QAction* menuHeatTransferAction2 = new QAction(QIcon(":/Physics/resources/Physics/domain.png"),tr("速度"));
    menuHeatTransfer->addAction(menuHeatTransferAction1);
    menuHeatTransfer->addAction(menuHeatTransferAction2);
    QAction* menuHeatTransferSeparator1 = new QAction();
    menuHeatTransferSeparator1->setSeparator(true);
    menuHeatTransfer->addAction(menuHeatTransferSeparator1);
    QAction* menuHeatTransferAction3 = new QAction(QIcon(":/Physics/resources/Physics/boundary.png"),tr("温度"));
    QAction* menuHeatTransferAction4 = new QAction(QIcon(":/Physics/resources/Physics/boundary.png"),tr("热绝缘"));
    QAction* menuHeatTransferAction5 = new QAction(QIcon(":/Physics/resources/Physics/boundary.png"),tr("热通量"));
    QAction* menuHeatTransferAction6 = new QAction(QIcon(":/Physics/resources/Physics/boundary.png"),tr("热源"));
    QAction* menuHeatTransferAction7 = new QAction(QIcon(":/Physics/resources/Physics/boundary.png"),tr("对流"));
    menuHeatTransfer->addAction(menuHeatTransferAction3);
    menuHeatTransfer->addAction(menuHeatTransferAction4);
    menuHeatTransfer->addAction(menuHeatTransferAction5);
    menuHeatTransfer->addAction(menuHeatTransferAction6);
    menuHeatTransfer->addAction(menuHeatTransferAction7);
    QAction* menuHeatTransferSeparator2 = new QAction();
    menuHeatTransferSeparator2->setSeparator(true);
    menuHeatTransfer->addAction(menuHeatTransferSeparator2);
    QAction* menuHeatTransferAction8 = new QAction(QIcon(":/Physics/resources/Physics/point.png"),tr("点热源"));
    menuHeatTransfer->addAction(menuHeatTransferAction8);
    connect(menuHeatTransferAction1, &QAction::triggered, this, &MainWindow::htAddHeatSource2D);
    connect(menuHeatTransferAction2, &QAction::triggered, this, &MainWindow::htAddVelocity);
    connect(menuHeatTransferAction3, &QAction::triggered, this, &MainWindow::htAddTemperature);
    connect(menuHeatTransferAction4, &QAction::triggered, this, &MainWindow::htAddThermalInsulation);
    connect(menuHeatTransferAction5, &QAction::triggered, this, &MainWindow::htAddHeatFlux);
    connect(menuHeatTransferAction6, &QAction::triggered, this, &MainWindow::htAddHeatSource1D);
    connect(menuHeatTransferAction7, &QAction::triggered, this, &MainWindow::htAddHeatConvection);
    connect(menuHeatTransferAction8, &QAction::triggered, this, &MainWindow::htAddHeatSource0D);

    // Mesh
    menuMesh = new QMenu(ui->treeView);
    menuMesh->setStyle(myIconStyle);
    QAction* menuMesh1 = new QAction(QIcon(":/Mesh/resources/Mesh/generateMesh.png"),tr("生成网格"));
    QAction* menuMesh2 = new QAction(QIcon(":/Mesh/resources/Mesh/clearMesh.png"),tr("清除网格"));
    menuMesh->addAction(menuMesh1);
    menuMesh->addAction(menuMesh2);
    QAction* menuMeshSeparator1 = new QAction();
    menuMeshSeparator1->setSeparator(true);
    menuMesh->addAction(menuMeshSeparator1);
    QAction* menuMesh3 = new QAction(QIcon(":/Mesh/resources/Mesh/size.png"),tr("尺寸"));
    QAction* menuMesh4 = new QAction(QIcon(":/Mesh/resources/Mesh/triangle.png"),tr("三角形"));
    QAction* menuMesh5 = new QAction(QIcon(":/Mesh/resources/Mesh/quadrilateral.png"),tr("四边形"));
    QAction* menuMesh6 = new QAction(QIcon(":/Mesh/resources/Mesh/edgeMesh.png"),tr("边网格"));
    menuMesh->addAction(menuMesh3);
    menuMesh->addAction(menuMesh4);
    menuMesh->addAction(menuMesh5);
    menuMesh->addAction(menuMesh6);
    connect(menuMesh3, &QAction::triggered, this, &MainWindow::addMeshSize);
    connect(menuMesh4, &QAction::triggered, this, &MainWindow::addTriangleMesh);
    connect(menuMesh5, &QAction::triggered, this, &MainWindow::addQuadMesh);
    connect(menuMesh6, &QAction::triggered, this, &MainWindow::addEdgeMesh);

    // Study
    menuSolver = new QMenu(ui->treeView);
    menuSolver->setStyle(myIconStyle);
    QAction* menuSolver1 = new QAction(QIcon(":/Study/resources/Study/calculate.png"),tr("计算"));
    QAction* menuSolver2 = new QAction(QIcon(":/Study/resources/Study/addStudy.png"),tr("添加研究"));
    QAction* menuSolver3 = new QAction(QIcon(":/Study/resources/Study/clearResult.png"),tr("清除结果"));
    menuSolver->addAction(menuSolver1);
    menuSolver->addAction(menuSolver2);
    menuSolver->addAction(menuSolver3);

    // Result
    menuResult = new QMenu(ui->treeView);
    menuResult->setStyle(myIconStyle);
    QAction* menuResult1 = new QAction(QIcon(":/Result/resources/Result/plot2D.png"),tr("添加二维绘图"));
    QAction* menuResult2 = new QAction(QIcon(":/Result/resources/Result/plot1D.png"),tr("添加一维绘图"));
    menuResult->addAction(menuResult1);
    menuResult->addAction(menuResult2);
    connect(menuResult1, &QAction::triggered, this, &MainWindow::addPlotGroup2D);
    connect(menuResult2, &QAction::triggered, this, &MainWindow::addPlotGroup1D);
    // Result, data set
    menuDataSet = new QMenu(ui->treeView);
    menuDataSet->setStyle(myIconStyle);
    QAction* menuDataSet1 = new QAction(QIcon(":/Result/resources/Result/pathDataSet.png"),tr("定义路径数据集"));
    QAction* menuDataSet2 = new QAction(QIcon(":/Result/resources/Result/pointDataSet.png"),tr("定义点数据集"));
    menuDataSet->addAction(menuDataSet1);
    menuDataSet->addAction(menuDataSet2);
    connect(menuDataSet1, &QAction::triggered, this, &MainWindow::addPathDataSet);
    connect(menuDataSet2, &QAction::triggered, this, &MainWindow::addPointDataSet);

    // Delete
    menuDelete = new QMenu(ui->treeView);
    menuDelete->setStyle(myIconStyle);
    QAction* menuDelete1 = new QAction(QIcon(":/Others/resources/Others/delete.png"),tr("删除"));
    menuDelete->addAction(menuDelete1);
    connect(menuDelete1, &QAction::triggered, this, &MainWindow::deleteItems);


}

// initialize status bar
void MainWindow::initStatusBar()
{
    ui->statusbar->showMessage("zhumin_phy@yeah.net", 0); // messange, seconds

    QProgressBar * qProgressBar = new QProgressBar();
    qProgressBar->setFixedSize(150,15);
    QLabel *qLabel= new QLabel();
    qLabel->setText("calculate progress:");
    qProgressBar->setRange(0,100);
    qProgressBar->setValue(30);
    //pStatusBar->addWidget(pLabel);//添加到状态栏的左边
    //pStatusBar->addWidget(pProgressBar);

    double usedMemory = cpuMemInfo::instance().usedMemory()/1024/1024/1024;
    double totalMemory = cpuMemInfo::instance().totalMemory()/1024/1024/1024;
    //QLabel *memLabel = new QLabel();
    _memLabel->setText("Mem "+QString::number(usedMemory,'f',1)+"/"+QString::number(totalMemory,'f',1)+" Total");

    ui->statusbar->addPermanentWidget(_memLabel);

    ui->statusbar->addPermanentWidget(qLabel);//添加到状态栏的右边
    ui->statusbar->addPermanentWidget(qProgressBar);

}

// initialize spliters
void MainWindow::initSpliter()
{
    ui->splitter->setStretchFactor(0,90);
    ui->splitter->setStretchFactor(1,10);
    ui->splitter_2->setStretchFactor(0,25);
    ui->splitter_2->setStretchFactor(1,25);
    ui->splitter_2->setStretchFactor(2,50);
}

// initialize model tree type tag list
void MainWindow::initTreeTagList()
{
    _treeTagList.push_back(TreeTag::DEFINE);
    _treeTagList.push_back(TreeTag::GEOMETRY);
    _treeTagList.push_back(TreeTag::MATERIAL);
    _treeTagList.push_back(TreeTag::HEAT_TRANSFER);
    _treeTagList.push_back(TreeTag::MESH);
    _treeTagList.push_back(TreeTag::STUDY);
    _treeTagList.push_back(TreeTag::RESULT);
}

void MainWindow::initConnect()
{
    connect(ui->comboBox, &QComboBox::currentIndexChanged, this, &MainWindow::setLengthUnit);
    connect(ui->comboBox_2, &QComboBox::currentIndexChanged, this, &MainWindow::setAngleUnit);
}


bool MainWindow::checkIndexDeletable(QModelIndexList &idxList)
{
    if (idxList.size() == 0) {
        return false;
    } else if (idxList.size() == 1) {
        if (idxList.first().data(Qt::UserRole).toBool())
            return true;
        else
            return false;
    } else {
        for (auto &idx: idxList) {
            if (!idx.data(Qt::UserRole).toBool())
                return false;
        }
        return true;
    }
}

// show tree view popup menu
void MainWindow::slotMenuPopup(const QPoint &pos)
{
    auto selectRowNum = ui->treeView->selectionModel()->selectedRows().size();
    QModelIndex curIndex = ui->treeView->indexAt(pos);
    if (curIndex.parent() == ui->treeView->rootIndex() && selectRowNum == 1) {
        switch(curIndex.row()) {
        case 0:
            menuDefinitions->exec(ui->treeView->mapToGlobal(pos));
            break;
        case 1:
            menuGeometry->exec(ui->treeView->mapToGlobal(pos));
            break;
        case 2:
            menuMaterials->exec(ui->treeView->mapToGlobal(pos));
            break;
        case 3:
            menuHeatTransfer->exec(ui->treeView->mapToGlobal(pos));
            break;
        case 4:
            menuMesh->exec(ui->treeView->mapToGlobal(pos));
            break;
        case 5:
            menuSolver->exec(ui->treeView->mapToGlobal(pos));
            break;
        case 6:
            menuResult->exec(ui->treeView->mapToGlobal(pos));
            break;
        default:
            break;
        }
    }

    // Result, dataSet
    if (curIndex.parent().row() == 6 && curIndex.row() == 0) {
        menuDataSet->exec(ui->treeView->mapToGlobal(pos));
    }

    /*
     * Delete
     * Popup delete menu when all selected indexes are 2nd or 3nd level,
     * and all indexes are deletable.
     */
    auto selIdx = ui->treeView->selectionModel()->selectedIndexes();
    if (checkIndexDeletable(selIdx)) {
        menuDelete->exec(ui->treeView->mapToGlobal(pos));
    }



/*
    bool delMatFlag = true;
    QModelIndexList indexes = ui->treeView->selectionModel()->selectedIndexes();
    foreach (QModelIndex index, indexes) {
        if (index.parent().row() != 2) {
            delMatFlag = false;
            break;
        }
    }
    if (delMatFlag)
        menuDeleteMaterial->exec(ui->treeView->mapToGlobal(pos));

    bool delCondFlag = true;
    foreach (QModelIndex index, indexes) {
        if (index.parent().row() != 3) {
            delCondFlag = false;
            break;
        }
        if (index.row() < 3) {
            delCondFlag = false;
            break;
        }
    }
    if (delCondFlag)
        menuDeleteCondition->exec(ui->treeView->mapToGlobal(pos));
*/
    //qDebug() << "parent_row = " << curIndex.parent().row() << ", parent_col = " << curIndex.parent().column() << Qt::endl;
    //qDebug() << "row = " << curIndex.row() << ", col = " << curIndex.column() << Qt::endl;

}

// click tree view and show settings
void MainWindow::slotTreeViewClick(const QModelIndex &index)
{
    //QModelIndex curIndex = ui->treeView->indexAt(pos);
    //int rowIdx = index.row();
    //int parentRowIdx = index.parent().row();
    qDebug() << ">> row = " << index.row() << ", parent_row = " << index.parent().row() << ", parent_parent_row = " << index.parent().parent().row() << ", tag = " << index.data(Qt::UserRole) << Qt::endl;
    if (index.data(Qt::UserRole+1) == TreeTag::nodeTag::DEFINE)
        qDebug() << "DEFINE" << Qt::endl;
    else if(index.data(Qt::UserRole+1) == TreeTag::nodeTag::GEOMETRY) {
        qDebug() << "GEOMETRY" << Qt::endl;
        if (index.data(Qt::UserRole+2) == TreeTag::geomTag::RECTANGLE) {
            qDebug() << "RECTANGLE" << Qt::endl;
            geomSetupRectangle *rect = new geomSetupRectangle();
            rect->geomSetup::setMenu(ui->page_3);
            ui->stackedWidget->setCurrentIndex(2);

            //connect(rect->pb_build, SIGNAL(clicked()), this, SLOT(test()));
        } else if (index.data(Qt::UserRole+2) == TreeTag::geomTag::SQUARE) {
            qDebug() << "SQUARE" << Qt::endl;
            geomSetupSquare *sqare = new geomSetupSquare();
            sqare->geomSetup::setMenu(ui->page_3);
            ui->stackedWidget->setCurrentIndex(2);
        } else if (index.data(Qt::UserRole+2) == TreeTag::geomTag::CIRCLE) {
            qDebug() << "CIRCLE" << Qt::endl;
            geomSetupCircle *circle = new geomSetupCircle();
            circle->geomSetup::setMenu(ui->page_3);
            ui->stackedWidget->setCurrentIndex(2);
        } else if (index.data(Qt::UserRole+2) == TreeTag::geomTag::ELLIPSE) {
            qDebug() << "ELLIPSE" << Qt::endl;
            geomSetupEllipse *ellipse = new geomSetupEllipse();
            ellipse->geomSetup::setMenu(ui->page_3);
            ui->stackedWidget->setCurrentIndex(2);
        } else if (index.data(Qt::UserRole+2) == TreeTag::geomTag::UNION) {
            qDebug() << "UNION" << Qt::endl;
            geomSetupUnion *gs_union = new geomSetupUnion();
            gs_union->geomSetup::setMenu(ui->page_3);
            ui->stackedWidget->setCurrentIndex(2);
        } else if (index.data(Qt::UserRole+2) == TreeTag::geomTag::INTERSECTION) {
            qDebug() << "INTERSECTION" << Qt::endl;
            geomSetupIntersection *intersect = new geomSetupIntersection();
            intersect->geomSetup::setMenu(ui->page_3);
            ui->stackedWidget->setCurrentIndex(2);
        } else if (index.data(Qt::UserRole+2) == TreeTag::geomTag::DIFFERENCE) {
            qDebug() << "DIFFERENCE" << Qt::endl;
            geomSetupDifference *diff = new geomSetupDifference();
            diff->geomSetup::setMenu(ui->page_3);
            ui->stackedWidget->setCurrentIndex(2);
        }
    } else if(index.data(Qt::UserRole+1) == TreeTag::nodeTag::MATERIAL)
        qDebug() << "MATERIAL" << Qt::endl;
    else if(index.data(Qt::UserRole+1) == TreeTag::nodeTag::MESH)
        qDebug() << "MESH" << Qt::endl;
    else if(index.data(Qt::UserRole+1) == TreeTag::nodeTag::STUDY)
        qDebug() << "STUDY" << Qt::endl;
    else if(index.data(Qt::UserRole+1) == TreeTag::nodeTag::RESULT)
        qDebug() << "RESULT" << Qt::endl;
    else {

    }

    /*
    // define
    if (index.data(Qt::UserRole+1) == TreeTag::DEFINE) {

        if (index.parent().parent().row() < 0) {
            // model information
            ui->stackedWidget->setCurrentIndex(0);
        } else {

        }

        if () {

        }
    }

    // geometry
    if (index.data(Qt::UserRole+1) == TreeTag::GEOMETRY) {
        if (index.parent().parent().row() < 0) {
            // unit setting
            ui->stackedWidget->setCurrentIndex(1);
        } else {

        }
    }
    */
}

void MainWindow::deleteNode(QModelIndex &idx)
{
    auto nodeTag = _model->itemFromIndex(idx)->text();
    if (idx.data(Qt::UserRole+1) == TreeTag::DEFINE) {
        _defineList.remove(nodeTag);
        return;
    }
    if (idx.data(Qt::UserRole+1) == TreeTag::GEOMETRY) {
        _shapeList.remove(nodeTag);
        return;
    }
    if (idx.data(Qt::UserRole+1) == TreeTag::MATERIAL) {
        _materialList.remove(nodeTag);
        return;
    }
    if (idx.data(Qt::UserRole+1) == TreeTag::HEAT_TRANSFER) {
        _htConditionList.remove(nodeTag);
        return;
    }
    if (idx.data(Qt::UserRole+1) == TreeTag::MESH) {
        _meshSettingList.remove(nodeTag);
        return;
    }
    if (idx.data(Qt::UserRole+1) == TreeTag::RESULT) {
        _plotGroupList.remove(nodeTag);
        _subDataSetList.remove(nodeTag);
        return;
    }
}

void MainWindow::deleteItems()
{
    auto selIdx = ui->treeView->selectionModel()->selectedIndexes();
    auto p = selIdx.rbegin();
    while (p != selIdx.rend()) {
        if ((*p).data(Qt::UserRole).toBool()) {
            QStandardItem *item = _model->itemFromIndex(*p)->parent();
            deleteNode(*p);
            item->removeRow((*p).row());
        }
        p++;
    }
}

/*
 * Add definitions
 */
void MainWindow::addNode(QSet<QString> &list, const int &itemIdx, const QString &namePrefix, const QIcon &icon)
{
    QString selectName;
    int i = 1;
    while (1) {
        bool inList = false;
        selectName = QString(namePrefix+" %1").arg(i);
        auto p = list.begin();
        while (p != list.end()) {
            if (*p == selectName) {
                inList = true;
                break;
            }
            p++;
        }
        if (!inList)
            break;
        i++;
    }
    _item = new QStandardItem(selectName);
    list << selectName;
    _item->setIcon(icon);
    int childIdx = list.size() - 1;
    _model->item(itemIdx,0)->setChild(childIdx, 0, _item);
    _model->item(itemIdx,0)->child(childIdx, 0)->setEditable(false);
    _model->item(itemIdx,0)->child(childIdx, 0)->setData(true, Qt::UserRole);
    _model->item(itemIdx,0)->child(childIdx, 0)->setData(_treeTagList[itemIdx], Qt::UserRole+1);
    ui->treeView->expand(ui->treeView->currentIndex());
}

void MainWindow::addParameter()
{
    QString namePrefix = "参数";
    QIcon icon = QIcon(":/Define/resources/Define/parameters.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::DEFINE) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_defineList, jNode, namePrefix, icon);

    int idx = _defineList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::defineTag::PARAMETER, Qt::UserRole+2);


    // parameter setting menu
    QWidget *page_3 = new QWidget();
    page_3->setObjectName("page_2");
    QWidget *layoutWidget1 = new QWidget(page_3);
    layoutWidget1->setObjectName("layoutWidget1");
    QGridLayout *gridLayout_P2 = new QGridLayout(layoutWidget1);

    gridLayout_P2->setObjectName("gridLayout_2");
    gridLayout_P2->setContentsMargins(0, 0, 0, 0);
    QLabel *label_3 = new QLabel(layoutWidget1);
    label_3->setObjectName("label_3");

    gridLayout_P2->addWidget(label_3, 0, 0, 1, 1);

    QCheckBox *checkBox = new QCheckBox(layoutWidget1);
    checkBox->setObjectName("checkBox");

    gridLayout_P2->addWidget(checkBox, 0, 1, 1, 1);

    QLabel *label_4 = new QLabel(layoutWidget1);
    label_4->setObjectName("label_4");

    gridLayout_P2->addWidget(label_4, 1, 0, 1, 1);

    QRadioButton *radioButton = new QRadioButton(layoutWidget1);
    radioButton->setObjectName("radioButton");

    gridLayout_P2->addWidget(radioButton, 1, 1, 1, 1);

    ui->stackedWidget->addWidget(page_3);
    ui->stackedWidget->setCurrentIndex(2);
}

void MainWindow::addFunction()
{
    QString namePrefix = "函数";
    QIcon icon = QIcon(":/Define/resources/Define/function.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::DEFINE) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_defineList, jNode, namePrefix, icon);

    int idx = _defineList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::defineTag::FUNCTION, Qt::UserRole+2);
}

void MainWindow::addSelectSet()
{
    QString namePrefix = "选择集";
    QIcon icon = QIcon(":/Define/resources/Define/select.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::DEFINE) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_defineList, jNode, namePrefix, icon);

    int idx = _defineList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::defineTag::SELECTION, Qt::UserRole+2);
}

/*
 * Add geometries
 */

void MainWindow::addRectangle()
{
    QString namePrefix = "长方形";
    QIcon icon = QIcon(":/Geometry/resources/Geometry/rectangle.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::GEOMETRY) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_shapeList, jNode, namePrefix, icon);

    int idx = _shapeList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::geomTag::RECTANGLE, Qt::UserRole+2);
}

void MainWindow::addSquare()
{
    QString namePrefix = "正方形";
    QIcon icon = QIcon(":/Geometry/resources/Geometry/square.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::GEOMETRY) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_shapeList, jNode, namePrefix, icon);

    int idx = _shapeList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::geomTag::SQUARE, Qt::UserRole+2);
}

void MainWindow::addCircle()
{
    QString namePrefix = "圆形";
    QIcon icon = QIcon(":/Geometry/resources/Geometry/circle.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::GEOMETRY) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_shapeList, jNode, namePrefix, icon);

    int idx = _shapeList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::geomTag::CIRCLE, Qt::UserRole+2);
}

void MainWindow::addEllipse()
{
    QString namePrefix = "椭圆形";
    QIcon icon = QIcon(":/Geometry/resources/Geometry/ellipse.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::GEOMETRY) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_shapeList, jNode, namePrefix, icon);

    int idx = _shapeList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::geomTag::ELLIPSE, Qt::UserRole+2);
}

void MainWindow::addPolygen()
{
    QString namePrefix = "正方形";
    QIcon icon = QIcon(":/Geometry/resources/Geometry/polygon.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::GEOMETRY) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_shapeList, jNode, namePrefix, icon);

    int idx = _shapeList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::geomTag::POLYGON, Qt::UserRole+2);
}

void MainWindow::addSegment()
{
    QString namePrefix = "线段";
    QIcon icon = QIcon(":/Geometry/resources/Geometry/segment.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::GEOMETRY) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_shapeList, jNode, namePrefix, icon);

    int idx = _shapeList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::geomTag::SEGMENT, Qt::UserRole+2);
}

void MainWindow::addArc()
{
    QString namePrefix = "圆弧";
    QIcon icon = QIcon(":/Geometry/resources/Geometry/arc.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::GEOMETRY) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_shapeList, jNode, namePrefix, icon);

    int idx = _shapeList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::geomTag::ARC, Qt::UserRole+2);
}

void MainWindow::addPoint()
{
    QString namePrefix = "点";
    QIcon icon = QIcon(":/Geometry/resources/Geometry/point.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::GEOMETRY) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_shapeList, jNode, namePrefix, icon);

    int idx = _shapeList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::geomTag::POINT, Qt::UserRole+2);
}

void MainWindow::addUnion()
{
    QString namePrefix = "并集";
    QIcon icon = QIcon(":/Geometry/resources/Geometry/union.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::GEOMETRY) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_shapeList, jNode, namePrefix, icon);

    int idx = _shapeList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::geomTag::UNION, Qt::UserRole+2);
}

void MainWindow::addIntersection()
{
    QString namePrefix = "交集";
    QIcon icon = QIcon(":/Geometry/resources/Geometry/intersection.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::GEOMETRY) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_shapeList, jNode, namePrefix, icon);

    int idx = _shapeList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::geomTag::INTERSECTION, Qt::UserRole+2);
}

void MainWindow::addDifference()
{
    QString namePrefix = "差集";
    QIcon icon = QIcon(":/Geometry/resources/Geometry/difference.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::GEOMETRY) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_shapeList, jNode, namePrefix, icon);

    int idx = _shapeList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::geomTag::DIFFERENCE, Qt::UserRole+2);
}

/*
 * Add materials
 */

void MainWindow::openMaterialDB()
{
    qDebug() << "Open Material Database" << Qt::endl;
}

void MainWindow::addMaterial()
{
    QString namePrefix = "材料";
    QIcon icon = QIcon(":/Material/resources/Material/material.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::MATERIAL) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_materialList, jNode, namePrefix, icon);

    //int idx = _materialList.size() - 1;
    //_model->item(jNode,0)->child(idx, 0)->setData(TreeTag::geomTag::RECTANGLE, Qt::UserRole+2);
}

/*
 * Add Heat Transfer conditions
 */

void MainWindow::htAddHeatSource2D()
{
    QString namePrefix = "热源";
    QIcon icon = QIcon(":/Physics/resources/Physics/domain.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::HEAT_TRANSFER) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_htConditionList, jNode, namePrefix, icon);

    int idx = _htConditionList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::htTag::HEAT_SOURCE_2D, Qt::UserRole+2);
}

void MainWindow::htAddVelocity()
{
    QString namePrefix = "速度";
    QIcon icon = QIcon(":/Physics/resources/Physics/domain.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::HEAT_TRANSFER) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_htConditionList, jNode, namePrefix, icon);

    int idx = _htConditionList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::htTag::VELOCITY, Qt::UserRole+2);
}

void MainWindow::htAddTemperature()
{
    QString namePrefix = "温度";
    QIcon icon = QIcon(":/Physics/resources/Physics/boundary.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::HEAT_TRANSFER) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_htConditionList, jNode, namePrefix, icon);

    int idx = _htConditionList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::htTag::TEMPERATURE, Qt::UserRole+2);
}

void MainWindow::htAddThermalInsulation()
{
    QString namePrefix = "热绝缘";
    QIcon icon = QIcon(":/Physics/resources/Physics/boundary.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::HEAT_TRANSFER) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_htConditionList, jNode, namePrefix, icon);

    int idx = _htConditionList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::htTag::THERMAL_INSULATION, Qt::UserRole+2);
}

void MainWindow::htAddHeatFlux()
{
    QString namePrefix = "热通量";
    QIcon icon = QIcon(":/Physics/resources/Physics/boundary.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::HEAT_TRANSFER) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_htConditionList, jNode, namePrefix, icon);

    int idx = _htConditionList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::htTag::HEAT_FLUX, Qt::UserRole+2);
}

void MainWindow::htAddHeatSource1D()
{
    QString namePrefix = "边热源";
    QIcon icon = QIcon(":/Physics/resources/Physics/boundary.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::HEAT_TRANSFER) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_htConditionList, jNode, namePrefix, icon);

    int idx = _htConditionList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::htTag::HEAT_SOURCE_1D, Qt::UserRole+2);
}

void MainWindow::htAddHeatConvection()
{
    QString namePrefix = "对流";
    QIcon icon = QIcon(":/Physics/resources/Physics/boundary.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::HEAT_TRANSFER) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_htConditionList, jNode, namePrefix, icon);

    int idx = _htConditionList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::htTag::HEAT_CONVECTION, Qt::UserRole+2);
}

void MainWindow::htAddHeatSource0D()
{
    QString namePrefix = "点热源";
    QIcon icon = QIcon(":/Physics/resources/Physics/point.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::HEAT_TRANSFER) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_htConditionList, jNode, namePrefix, icon);

    int idx = _htConditionList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::htTag::HEAT_SOURCE_0D, Qt::UserRole+2);
}

/*
 * Add mesh setting
 */

void MainWindow::addMeshSize()
{
    QString namePrefix = "尺寸";
    QIcon icon = QIcon(":/Mesh/resources/Mesh/size.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::MESH) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_meshSettingList, jNode, namePrefix, icon);

    int idx = _meshSettingList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::meshTag::SIZE, Qt::UserRole+2);
}

void MainWindow::addTriangleMesh()
{
    QString namePrefix = "三角形";
    QIcon icon = QIcon(":/Mesh/resources/Mesh/triangle.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::MESH) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_meshSettingList, jNode, namePrefix, icon);

    int idx = _meshSettingList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::meshTag::TRIANGLE, Qt::UserRole+2);
}

void MainWindow::addQuadMesh()
{
    QString namePrefix = "四边形";
    QIcon icon = QIcon(":/Mesh/resources/Mesh/quadrilateral.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::MESH) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_meshSettingList, jNode, namePrefix, icon);

    int idx = _meshSettingList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::meshTag::QUAD, Qt::UserRole+2);
}

void MainWindow::addEdgeMesh()
{
    QString namePrefix = "边网格";
    QIcon icon = QIcon(":/Mesh/resources/Mesh/edgeMesh.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::MESH) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_meshSettingList, jNode, namePrefix, icon);

    int idx = _meshSettingList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::meshTag::EDGE, Qt::UserRole+2);
}

/*
 * Add data set
 */

void MainWindow::addDataSet(QSet<QString> &list, const int &itemIdx, const QString &namePrefix, const QIcon &icon)
{
    QString selectName;
    int i = 1;
    while (1) {
        bool inList = false;
        selectName = QString(namePrefix+" %1").arg(i);
        auto p = list.begin();
        while (p != list.end()) {
            if (*p == selectName) {
                inList = true;
                break;
            }
            p++;
        }
        if (!inList)
            break;
        i++;
    }
    _item = new QStandardItem(selectName);
    list << selectName;
    _item->setIcon(icon);
    int defineIdx = list.size() - 1;
    _model->item(itemIdx,0)->child(0,0)->setChild(defineIdx, 0, _item);
    _model->item(itemIdx,0)->child(0,0)->child(defineIdx, 0)->setEditable(false);
    _model->item(itemIdx,0)->child(0,0)->child(defineIdx, 0)->setData(true, Qt::UserRole);
    _model->item(itemIdx,0)->child(0,0)->child(defineIdx, 0)->setData(TreeTag::RESULT, Qt::UserRole+1);
    ui->treeView->expand(ui->treeView->currentIndex());
}

void MainWindow::addPathDataSet()
{
    QString namePrefix = "路径数据集";
    QIcon icon = QIcon(":/Result/resources/Result/pathDataSet.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::RESULT) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addDataSet(_subDataSetList, jNode, namePrefix, icon);

    int idx = _subDataSetList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::resultTag::PATHSET, Qt::UserRole+2);
}

void MainWindow::addPointDataSet()
{
    QString namePrefix = "点数据集";
    QIcon icon = QIcon(":/Result/resources/Result/pointDataSet.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::RESULT) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addDataSet(_subDataSetList, jNode, namePrefix, icon);

    int idx = _subDataSetList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::resultTag::POINTSET, Qt::UserRole+2);
}

/*
 * Add plot group
 */

void MainWindow::addPlotGroup2D()
{
    QString namePrefix = "二维绘图组";
    QIcon icon = QIcon(":/Result/resources/Result/plot2D.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::RESULT) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_plotGroupList, jNode, namePrefix, icon);

    int idx = _plotGroupList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::resultTag::FIG2D, Qt::UserRole+2);
}

void MainWindow::addPlotGroup1D()
{
    QString namePrefix = "一维绘图组";
    QIcon icon = QIcon(":/Result/resources/Result/plot1D.png");
    auto iNode = std::find(_treeTagList.begin(),_treeTagList.end(),TreeTag::RESULT) - _treeTagList.begin();
    auto jNode = static_cast<int>(iNode);
    addNode(_plotGroupList, jNode, namePrefix, icon);

    int idx = _plotGroupList.size() - 1;
    _model->item(jNode,0)->child(idx, 0)->setData(TreeTag::resultTag::FIG1D, Qt::UserRole+2);
}

void MainWindow::deleteDefinition()
{

}

void MainWindow::deleteShape()
{

}

void MainWindow::deleteMaterial()
{
    QModelIndex curIndex = ui->treeView->currentIndex();

    qDebug() << ">> parent_row = " << curIndex.parent().row() << ", parent_col = " << curIndex.parent().column() << Qt::endl;
    if (curIndex.parent().row() == 2) {
        // delete material
        _model->item(2,0)->removeRow(curIndex.row());
        qDebug() << "bbbbbbb" << Qt::endl;
    }

}

void MainWindow::deleteCondition()
{

}

void MainWindow::deleteDataSet()
{

}

void MainWindow::deletePlot()
{

}

// Press DELETE to remove nodes
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == ui->treeView) {
        if (event->type() == QEvent::KeyPress) {
            QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
            if (keyEvent->key() == Qt::Key_Delete)
                deleteItems();
        }
    }
    return QWidget::eventFilter(obj, event);
}

/*
 * model information setting
 */
void MainWindow::setTitle(QString str)
{

}

void MainWindow::setAuthor(QString str)
{

}

void MainWindow::setDescription(QString str)
{

}

/*
 * unit setting
 */
void MainWindow::setLengthUnit(int index)
{
    globalSet->setGeomUnit(static_cast<geomUnitType>(index));
}

void MainWindow::setAngleUnit(int index)
{
    globalSet->setAngleUnit(static_cast<angleUnitType>(index));
}

/*
 * test function
 */
void MainWindow::test()
{
    qDebug() << "Rectangle build button pushed!" << Qt::endl;
}
